package com.scalar.chiptrack.sales.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;

public class CustomerRelationshipDisplayAction extends ChipTrackAction{

    private static final String ACTION_NAME = "/customerRelationship.do";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
    						HttpServletResponse response) throws Exception {
    	
    	String action = request.getParameter("action");
    	String customerId = request.getParameter("customerId");
    	
    	//Check For user Permissions before going further
		if(!isUserHasValidSession(mapping, request)){
			
			return (mapping.findForward ("login"));
		}
		
		if("editcustomer".equalsIgnoreCase(action)){
			return (new CustomerRelationshipEditAction()).execute(mapping, form, request, response);
		}

        if("ViewCustomer".equalsIgnoreCase(action)){
            request.setAttribute("action","view");
			return (new CustomerRelationshipEditAction()).execute(mapping, form, request, response);
		}


		if("deletecustomer".equalsIgnoreCase(action)){
		}
		
    	return null;
	}
	
	//Check For user sessions and permissions
	//This is pulled from CustomerListAction.java
	public boolean isUserHasValidSession(ActionMapping mapping, HttpServletRequest request) throws Exception{
		
		//Get the session, to retrieve the values
	    HttpSession session = request.getSession();
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);
        
        if(user == null)
        {
	    	session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME+ "?" + request.getQueryString());

	    	return false;
        }

        String showModify = "FALSE"; String showDelete = "FALSE"; String showView = "FALSE";

        if(user.isActionAllowed(Functionality.CUSTOMER_RELATIONSHIPS,User.USER_ACTION_MODIFY))
        	showModify = "TRUE";
        if(user.isActionAllowed(Functionality.CUSTOMER_RELATIONSHIPS,User.USER_ACTION_DELETE))
        	showDelete = "TRUE";

        request.setAttribute("showModify",showModify);
        request.setAttribute("showDelete",showDelete);
        
        return true;
	}
}